import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:jl_plugin/bean/KeySettings.dart';
import 'package:jl_plugin/jl_plugin.dart';
import 'package:jl_plugin_example/AppBarLeftTitle.dart';
import 'package:jl_plugin/bean/KeyOption.dart';
import 'package:jl_plugin_example/widget/AppColors.dart';

class BlueKeySettingPage extends StatefulWidget{
  @override
  State<StatefulWidget> createState() {
    return _State();
  }
}

class _State extends State<BlueKeySettingPage>{
  List<KeySetting> keySettingsListSelect=[];
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBarLeftTitle(
        title: "耳机按键设置",
      ),
      body: SingleChildScrollView(
        child: Container(
          padding: EdgeInsets.all(15),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                '左耳',
                style: const TextStyle(fontSize: 16, fontWeight: FontWeight.w600),
              ),
              FunList("短按耳机",keyFunctionList,1,1),
              FunList("轻点两下耳机",keyFunctionList,1,2),
              Text(
                '右耳',
                style: const TextStyle(fontSize: 16, fontWeight: FontWeight.w600),
              ),
              FunList("短按耳机",keyFunctionList,2,1),
              FunList("轻点两下耳机",keyFunctionList,2,2),


            ],
          ),
        ),
      ),
    );
  }

  Widget FunList(String title,List<KeyOption> optionList,int keyNum,int action){

    return Container(
      margin: EdgeInsets.symmetric(vertical: 10),
      padding: EdgeInsets.symmetric(horizontal: 15),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            '${title}：',
            style: const TextStyle(fontSize: 16, fontWeight: FontWeight.w600),
          ),
          SizedBox(height: 10),
          Wrap(
            spacing: 8,
            runSpacing: 8,
            children: optionList.map((func) {
              bool isCheck=keySettingsListSelect.any((item){
                return item.keyNum==keyNum&&item.action==action&&item.function==func.value;
              });

              return GestureDetector(
                onTap: (){
                  if(!isCheck){
                    var currentBean=keySettingsListSelect.where((item)=>item.keyNum==keyNum&&item.action==action).firstOrNull;
                    if(currentBean!=null){
                      currentBean.function=func.value;
                    }else{
                      currentBean=KeySetting(keyNum: keyNum,action: action,function: func.value);
                      keySettingsListSelect.add(currentBean);
                    }
                    setState(() {

                    });
                    changeKeySetting(keySettingsListSelect);
                  }
                },
                child: Chip(
                  shape: StadiumBorder(
                    side: BorderSide(
                      color: isCheck?Colors.green:c_999999, // 边框颜色
                      width: 1,           // 边框宽度
                    ),
                  ),
                  label: Text(func.zh,style: TextStyle(color: isCheck?Colors.green:c_999999),),
                ),
              );
            }).toList(),
          ),
        ],
      ),
    );
  }

  @override
  void initState() {
    super.initState();
    getKeySetting();
  }

  void getKeySetting(){
    JlPlugin.instance.deviceSettingInfo(0xffffffff, successCall: (info){
      print("device Info:${jsonEncode(info.keySettingsList)}");
      keySettingsListSelect.clear();
      keySettingsListSelect.addAll(info.keySettingsList);
      setState(() {

      });

    }, onErrorCall: (errorResult){
      print("device onErrorCall:${errorResult.toString()}");
    });
  }

  void changeKeySetting(List<KeySetting> setting) async{
    var result=await JlPlugin.instance.configKeySettings(setting);
    print("changeKeySetting:${result.toString()}");
  }
}